import axios from 'axios';
import Qs from 'qs'
import {config} from '../../config';
import { ref } from 'vue';

let answer = ref('')
let receivedBuffer = '';
export const chatAIserver = {
    async getRecReading(){
        try {
            const response = await axios.post(
                config+'/api/streamConsult',
                { message: 'example' },
                {
                  responseType: 'stream', // 必须声明流式响应类型
                  onDownloadProgress: (progressEvent) => {
                    // 实时处理数据块
                    const chunk = progressEvent.event.target.response;
                    answer.value += chunk;
                    console.log("【流式数据返回】",answer.value)
                  }
                }
              );
              
        }catch(err) {
            console.log('【请求错误】', err);
            return { success: false };
        }
    },
    //DS Chat
    async getDSChat(message){

        await axios({
            method: 'post',
            url: config+'/api/getStreamChat', // 替换为实际API
            responseType: 'text',
            headers: {
                "Content-Type": "application/x-www-form-urlencoded" // 使用JSON格式
            },
            data: {
                message: message
            },
            onDownloadProgress: (progressEvent) => {
                // 获取新数据块
                const newText = progressEvent.event.currentTarget.responseText;
                
                // 计算新增内容
                const newContent = newText.substring(receivedBuffer.length);
                receivedBuffer = newText;
                
                // 处理新内容
                if (newContent) {
                    // const messageElement = document.createElement('div');
                    // messageElement.className = 'message';
                    // messageElement.textContent = `数据块 ${++chunkCount}: ${newContent}`;
                    // responseContent.appendChild(messageElement);
                    // responseContent.scrollTop = responseContent.scrollHeight;
                    // updateReceivedCount();

                    // console.log("")
                    
                    console.log('收到数据块:', newContent);
                    // return newContent;
                }
            }
        });

    },
    
    async addMessage(message,is_robot) {
        try {
            const response = await axios.post(config + '/api/addChatHistory',{ // 请求体数据 要放在第二个参数
                message: message,
                is_robot: is_robot
            },
            { // 配置选项   配置项作为第三个参数
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    // 'X-Requested-With': 'XMLHttpRequest'
                },
                withCredentials: false // 携带跨域cookie
            }
            )
            const data = response.data;
            console.log('【返回结果】', response);
            return {success:true, data:data};
        }catch(err) {
            console.log('【请求错误】', err);
            return { success: false };
        }
    }
}

